package com.deep.seckill.schedule;

import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * 定时任务
 * <p>
 * 异步任务：1、@EnableAsync 开启异步任务功能 2、@Async异步执行该方法
 * TaskExecutionAutoConfiguration 线程coreSize = 8
 *
 * @author Deep
 * @date 2022/2/21
 */
@Slf4j
//@Component
//@EnableAsync    // 开启异步任务功能
//@EnableScheduling   // 开启定时任务
public class TestSchedule {
    @Async
    @Scheduled(cron = "* * * * * ?")    // 默认是单线程执行的（会发生阻塞）
    public void test() {
        try {
            log.info("Hello,this is a schedule test......" + LocalDateTime.now().getSecond());
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
//        CompletableFuture.runAsync(() -> {
//            try {
//                log.info("Hello,this is a schedule test......" + LocalDateTime.now().getSecond());
//                Thread.sleep(5000);
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
//        });
    }
}
